package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.LawsuitMapper;
import com.csr.csrcheck.pojo.Lawsuit;
import com.csr.csrcheck.service.LawsuitService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.LawsuitException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LawsuitServiceImpl implements LawsuitService {
    @Resource
    private LawsuitMapper lawsuitMapper;

    public List<Lawsuit> getLawsuitlist() {
        List<Lawsuit> list = this.lawsuitMapper.getLawsuitlist();
        if (list == null)
            throw new LawsuitException("");
        return list;
    }

    public PageResult getlistpage(int pageNum, int pageSize, String company_name, String casetype, String doctype, String defendants, String submittime) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, company_name, casetype, doctype, defendants, submittime));
    }

    public int addlawsuit(Lawsuit lawsuit) {
        int row = this.lawsuitMapper.addlawsuit(lawsuit);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int updatelawsuit(Lawsuit lawsuit) {
        int row = this.lawsuitMapper.updatelawsuit(lawsuit);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int deletelawsuit(int id) {
        int row = this.lawsuitMapper.deletelawsuit(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Lawsuit findlawsuit(int id) {
        return this.lawsuitMapper.findlawsuit(id);
    }

    private PageInfo<Lawsuit> getPageInfo(int pageNum, int pageSize, String company_name, String casetype, String doctype, String defendants, String submittime) {
        PageHelper.startPage(pageNum, pageSize);
        List<Lawsuit> list = this.lawsuitMapper.getListpage(company_name, casetype, doctype, defendants, submittime);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
